package com.vlong.town.controller.server.wxUser;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.vlong.town.controller.base.BaseController;
import com.vlong.town.entity.base.DataMessage;
import com.vlong.town.entity.wxUser.WxUser;
import com.vlong.town.service.wxUser.WxUserService;
import com.vlong.town.util.common.TownConstant;
/**
 * 微信用户Rest接口Controller
 * @author Assassin
 */
@RequestMapping("/rest/wxUserInfo")
@RestController
public class WxUserRestController extends BaseController{

	@Autowired
	private WxUserService wxUserService;
	
	/**
     * 微信用户新增接口
     * @param wxName 微信名称
     * @param wxId 微信id
     * @param telphone 微信手机号
     * @return DataMessage 消息实体
     */
	@ResponseBody
    @PostMapping("/addwxUser")
    public DataMessage addwxUser(
    		@RequestParam(required=false, value="wxName") String wxName,
    		@RequestParam(required=false, value="wxId") String wxId,
    		@RequestParam(required=false, value="telphone") String telphone
    		) {
    	DataMessage  dm=new  DataMessage();	
    	if(!validateStringProperty(wxName, 1, 128)){
    		dm.setResult(TownConstant.RESULT_CODE_DATA_ERROR);
    		dm.setMessage("微信名称参数错误");
    	}else  if(!validateStringProperty(wxId, 1, 128)){
    		dm.setResult(TownConstant.RESULT_CODE_DATA_ERROR);
    		dm.setMessage("微信id参数错误");
    	}else  if(!validateStringProperty(telphone, 11, 20)){
    		dm.setResult(TownConstant.RESULT_CODE_DATA_ERROR);
    		dm.setMessage("微信手机号参数错误");
    	}else{
    		List<WxUser> wxUserList = wxUserService.queryList(null, 1, 1, 1L, wxId, null, null, null, null, null, null,telphone, null, null, null, null, null);
    		if(wxUserList!=null&&wxUserList.size()>0){
    			dm.setResult(TownConstant.RESULT_CODE_DATA_ERROR);
    			dm.setMessage("注册失败");
    		}else{
    			wxUserService.add(null, 1, 1, 1L, wxId, wxName, null, null, null, null, null,telphone, null, null, null, null, System.currentTimeMillis(), null);
    			dm.setResult(TownConstant.RESULT_CODE_SUCCESS);
    			dm.setMessage("注册成功");
    		}
    	}
    	return  dm;
    }
    
    /**
     * 微信用户编辑接口
     * @param wxId 微信id
     * @param realName 用户名称
     * @param telphone 微信手机号
     * @param email 邮箱
     * @return DataMessage 消息实体
     */
	@ResponseBody
    @PostMapping("/editWxUser")
    public  DataMessage  editWxUser(
    		@RequestParam(required=true, value="wxId") String wxId,
    		@RequestParam(required=true, value="realName") String realName,
    		@RequestParam(required=true, value="telphone") String telphone,
    		@RequestParam(required=true, value="email") String email
    		){
    	DataMessage  dm=new  DataMessage();	
    	wxUserService.edit(null, null, null, null, wxId, null, null, null, realName, null,null,telphone, email, null, null, null, null, System.currentTimeMillis());
    	dm.setResult(TownConstant.RESULT_CODE_SUCCESS);
		dm.setMessage("编辑成功");
    	return  dm;
    }
	
	
	/**
     * 查询个人信息详情
     * @param id 微信id
     * @return WxUser
     */
    @GetMapping("/detail/{wxId}")
    public WxUser gotoWxUserPage(@PathVariable(required=true, value="wxId") String wxId) {
        return this.wxUserService.queryByWxId(wxId);
    }
}
